export const baseURL = "http://www.zhanniudajiankang.com/api"; //云蓬域名
export const request = (url, method, data) => {
  let addData = {};
  console.log("请求接口地址", url);
  if (!url.includes("cart/count")) {
    uni.showLoading({
      title: '加载中...'
    })
  }

  // data没有code的接口
  return new Promise((resolve, reject) => {
      console.log("进来了");
      uni.request({
          url: baseURL + url, //接口地址：前缀+方法中传入的地址
          method: method || "GET", //请求方法：传入的方法或者默认是“GET”
          data: Object.assign(data, addData) || {}, //传递参数：传入的参数或者默认传递空集合
          header: {
            "Accept-Language": "en",
            "Authori-zation": 'Bearer ' + uni.getStorageSync("token"), //自定义请求头信息
          },
          success: (res) => {
            console.log('requestData', Object.assign(data, addData))
            console.log('response', res);
            if (res.data.status == 200) {
              setTimeout(() => {
                uni.hideLoading({
                  noConflict: true
                });
              }, 300)
              resolve(res.data);
            } else if([110002, 110003, 110004].indexOf(res.data.status) !== -1) {
              console.log("登录失效");
              uni.clearStorageSync();
              uni.reLaunch({
                url: "/pages/home/login"
              })
            } else {
              uni.showToast({
                icon: "none",
                title: res.data.msg,
                duration: 1500
              });
            }
            setTimeout(() => {
              uni.hideLoading({
                noConflict: true
              });
            }, 1500)
            reject(res.data);
        },
        // 这里的接口请求，如果出现问题就输出接口请求失败
        fail: (err) => {
          console.log("error", err);
          uni.showToast({
            title: '系统错误',
            icon: 'error'
          })
          setTimeout(() => {
            uni.hideLoading({
              noConflict: true
            });
          }, 300)
          reject(err);
        },
      });
  });
};